/*******************************************************************************
 * Copyright (c) 2002 Boris Pruessmann and others. All rights reserved. This
 * program and the accompanying materials are made available under the terms of
 * the Common Public License v0.5 which accompanies this distribution, and is
 * available at http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors: Boris Pruessmann - Initial implementation.
 ******************************************************************************/
package net.sourceforge.perforce.ui.internal.dnd;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;

import java.util.Iterator;
import java.util.List;
import java.util.Set;

import net.sourceforge.perforce.core.api.IPerforceServer;
import net.sourceforge.perforce.core.resources.IP4File;

import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.dnd.DragSourceAdapter;
import org.eclipse.swt.dnd.DragSourceEvent;

/**
 * @version $Revision: 488 $
 * @author <a href="mailto:bpruessmann@users.sourceforge.net">Boris
 *         Pruessmann</a>
 */
public class PendingChangelistDragAdapter extends DragSourceAdapter {
  // ---------------------------------------------------------------------------
  // Private Variables

  /** Stores the selection provider. */
  private ISelectionProvider selectionProvider;

  // -------------------------------------------------------------------------
  // Public Constructors

  /**
   * Constructor for PendingChangelistDragAdapter.
   * 
   * @param provider the selection provider
   */
  public PendingChangelistDragAdapter(ISelectionProvider provider) {
    selectionProvider = provider;
  }

  //----------------------------------------------------------------------------
  // -- Public Methods

  @Override
  public void dragSetData(DragSourceEvent event) {
    if (LocalFileTransfer.getInstance().isSupportedType(event.dataType)) {
      event.data = getSelectedLocalFiles();
    }
  }

  @Override
  public void dragStart(DragSourceEvent event) {
    Set<IPerforceServer> serverSet = Sets.newHashSet();
    IStructuredSelection selection = (IStructuredSelection) selectionProvider.getSelection();
    for (Iterator<?> it = selection.iterator(); it.hasNext();) {
      Object next = it.next();
      if (!(next instanceof IP4File)) {
        event.doit = false;
        return;
      } else {
        IP4File file = (IP4File) next;
        IPerforceServer server = file.getServer();
        if (!file.getClientName().equals(server.getClient())) {
          event.doit = false;
          return;
        }

        serverSet.add(server);
      }
    }

    event.doit = serverSet.size() == 1;
  }

  //----------------------------------------------------------------------------
  // - Private Methods

  /**
   * Returns the currently selected LocalFiles.
   * 
   * @return array of selected LocalFiles.
   */
  private Object getSelectedLocalFiles() {
    IStructuredSelection selection = (IStructuredSelection) selectionProvider.getSelection();
    if (selection == null || selection.isEmpty()) {
      return null;
    }

    List<IP4File> result = Lists.newArrayList();
    for (Iterator<?> it = selection.iterator(); it.hasNext();) {
      Object next = it.next();
      if (next instanceof IP4File) {
        result.add((IP4File) next);
      }
    }

    return result.toArray(new IP4File[result.size()]);
  }
}
